package com.example.arithmeticleetcode.leetcode.September;

import java.util.ArrayDeque;
import java.util.HashSet;
import java.util.Set;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-09-16 09:11
 **/
public class Demo13 {

    public static void main(String[] args) {
        String[] emails1 = {"test.email+alex@leetcode.com","test.e.mail+bob.cathy@leetcode.com","testemail+david@lee.tcode.com"};
        System.out.println(numUniqueEmails(emails1));
        String[] emails2 = {"test.email+alex@leetcode.com", "test.email@leetcode.com"};
        System.out.println(numUniqueEmails(emails2));
    }

    public static int numUniqueEmails(String[] emails) {
        Set<String> strings = new HashSet<>();
        for (String email : emails) {
            StringBuilder stringBuilder = new StringBuilder();
            String[] split = email.split("\\+");
            String[] split1 = email.split("\\@");
            if (split.length > 1) {
                String[] split2 = split[0].split("\\.");
                for (String s : split2) {
                    stringBuilder.append(s);
                }
            } else {
                String[] split2 = split1[0].split("\\.");
                for (String s : split2) {
                    stringBuilder.append(s);
                }
            }
            stringBuilder.append("@").append(split1[split1.length - 1]);
            strings.add(stringBuilder.toString());
        }
        return strings.size();
    }
}
